package work.parabola.keyboardswitch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.LoginQuery;
import work.parabola.keyboardswitch.service.IUserService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @PostMapping("login.do")
    public ServerResponse login(HttpSession session, @RequestBody LoginQuery loginQuery) {
        return this.iUserService.login(session, loginQuery);
    }

    @PostMapping("checkLogin.do")
    public ServerResponse checkLogin(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        return ServerResponse.createBySuccess(userSession);
    }

    @PostMapping("logout.do")
    public ServerResponse logout(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if(userSession != null){
            session.invalidate();
        }
        return ServerResponse.createBySuccessMessage("登出成功");
    }

}
